<?php


namespace App\Http\Controllers\Admin;

use App\Facade\AppContext;
use App\Models\Ca\Customer;
use App\Models\Ca\Source;
use App\Utils\TimeUtil;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;

class ConsoleController extends BaseController
{
    public function __construct()
    {
        $this->set('console');
    }

    public function workplace()
    {
        return $this->view('workplace');
    }

    public function index()
    {
        $pageName = '首页';
        if (AppContext::can('index', 'analysis/home')) {
            $sources = Source::query()->where('hospital_id', '=', AppContext::hospitalId())->get(['id', 'name']);
            $map = [];
            foreach ($sources as $source) {
                $map[$source['id']] = $source['name'];
            }

            $tj['dj'] = $this->query('created_at', $map);
            $tj['yy'] = $this->query('yy_time', $map);
            $tj['jz'] = $this->query('jz_time', $map);
            $tj['spend'] = $this->query('spend_time', $map);
        } else {
            $tj = [];
        }
        return $this->view('index', compact('pageName', 'tj'));
    }

    public function query($field, $map)
    {
        $query = Customer::query()
            ->where('hospital_id', '=', AppContext::hospitalId())
            ->groupBy('source_id');

        $query->select('source_id');
        $range = TimeUtil::today();
        $start = date('Y-m-d H:i:s', $range['begin']);
        $end = date('Y-m-d H:i:s', $range['end']);
        $query->selectRaw("sum(case when $field between '$start' and '$end' then 1 else 0 end) today");
        $range = TimeUtil::yesterday();
        $start = date('Y-m-d H:i:s', $range['begin']);
        $end = date('Y-m-d H:i:s', $range['end']);
        $query->selectRaw("sum(case when $field between '$start' and '$end' then 1 else 0 end) yesterday");

        $range = TimeUtil::thisWeek();
        $start = date('Y-m-d H:i:s', $range['begin']);
        $end = date('Y-m-d H:i:s', $range['end']);
        $query->selectRaw("sum(case when $field between '$start' and '$end' then 1 else 0 end) week");
        $range = TimeUtil::lastWeek();
        $start = date('Y-m-d H:i:s', $range['begin']);
        $end = date('Y-m-d H:i:s', $range['end']);
        $query->selectRaw("sum(case when $field between '$start' and '$end' then 1 else 0 end) last_week");

        $range = TimeUtil::thisMonth();
        $start = date('Y-m-d H:i:s', $range['begin']);
        $end = date('Y-m-d H:i:s', $range['end']);
        $query->selectRaw("sum(case when $field between '$start' and '$end' then 1 else 0 end) month");
        $range = TimeUtil::lastMonth();
        $start = date('Y-m-d H:i:s', $range['begin']);
        $end = date('Y-m-d H:i:s', $range['end']);
        $query->selectRaw("sum(case when $field between '$start' and '$end' then 1 else 0 end) last_month");
        $query->where($field, '>=', $start);
        $list = $query->get();

        foreach ($list as $item) {
            $item['source_name'] = Arr::get($map, $item['source_id'], 0);
        }
        return $list;
    }
}
